<?php

/**
 * 获取一定范围内的随机数
 *
 * @param unknown $min 最小范围
 * @param unknown $max 最大范围
 * @return string 随机数
 */
function common_rand_number($min, $max) {
	return sprintf ( "%0" . strlen ( $max ) . "d", mt_rand ( $min, $max ) );
}
/**
 * 验证eamil
 *
 * @param string $value 需要验证的邮箱地址        	
 * @param int $length
 * @return boolean
 */
function common_is_email($value, $match = '/^[\w\d]+[\w\d-.]*@[\w\d-.]+\.[\w\d]{2,10}$/i') {
	$v = trim ( $value );
	if (empty ( $v ))
		return false;
	return preg_match ( $match, $v );
}

/**
 * 验证电话号码
 *
 * @param string $value        	
 * @return boolean
 */
function common_is_telephone($value, $match = '/^0[0-9]{2,3}[-]?\d{7,8}$/') {
	$v = trim ( $value );
	if (empty ( $v ))
		return false;
	return preg_match ( $match, $v );
}

/**
 * 验证手机
 *
 * @param string $value        	
 * @param string $match        	
 * @return boolean
 */
function common_is_mobile($value, $match = '/^[(\+86)|0]?(13\d{9})|(15\d{9})|(18\d{9})$/') {
	$v = trim ( $value );
	if (empty ( $v ))
		return false;
	return preg_match ( $match, $v );
}

/**
 * 验证邮政编码
 *
 * @param string $value        	
 * @param string $match        	
 * @return boolean
 */
function common_is_postcode($value, $match = '/\d{6}/') {
	$v = trim ( $value );
	if (empty ( $v ))
		return false;
	return preg_match ( $match, $v );
}
/**
 * 验证验证码
 * @param unknown $value 验证码
 * @param number $strLen 验证码长度
 * @param string $charset 验证类型，CN,EN，分别表示中文或英文,NU表示数字
 * @return boolean|number 验证结果
 */
function common_is_verifycode($value, $strLen = 2,$charset = 'ALL') {
	if (empty ( $value ))
		return false;
	switch ($charset) {
		case 'EN' :
			$match = '/^[_\w\d]{' . $strLen . '}$/iu';
			break;
		case 'NU' :
				$match = '/^[_\d]{' . $strLen . '}$/iu';
				break;
		case 'CN' :
			$match = '/^[_\x{4e00}-\x{9fa5}\d]{' . $strLen . '}$/iu';
			break;
		default :
			$match = '/^[_\w\d\x{4e00}-\x{9fa5}]{' . $strLen . '}$/iu';
	}
	return preg_match ( $match, $value );
}
/**
 * 验证IP
 *
 * @param string $value        	
 * @param string $match        	
 * @return boolean
 */
function common_is_ip($value, $match = '/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/') {
	$v = trim ( $value );
	if (empty ( $v ))
		return false;
	return preg_match ( $match, $v );
}

/**
 * 验证身份证号码
 *
 * @param string $value        	
 * @param string $match        	
 * @return boolean
 */
function common_is_idcard($value, $match = '/^\d{6}((1[89])|(2\d))\d{2}((0\d)|(1[0-2]))((3[01])|([0-2]\d))\d{3}(\d|X)$/i') {
	$v = trim ( $value );
	if (empty ( $v ))
		return false;
	else if (strlen ( $v ) > 18)
		return false;
	return preg_match ( $match, $v );
}

/**
 * *
 * 验证URL
 *
 * @param string $value        	
 * @param string $match        	
 * @return boolean
 */
function common_is_url($value, $match = '/^(http:\/\/)?(https:\/\/)?([\w\d-]+\.)+[\w-]+(\/[\d\w-.\/?%&=]*)?$/') {
	$v = strtolower ( trim ( $value ) );
	if (empty ( $v ))
		return false;
	return preg_match ( $match, $v );
}

/**
 * 验证用户名
 *
 * @param string $value        	
 * @param int $length        	
 * @return boolean
 */
function common_is_names($value, $minLen = 2, $maxLen = 20, $charset = 'ALL') {
	if (empty ( $value ))
		return false;
	switch ($charset) {
		case 'EN' :
			$match = '/^[_\w\d]{' . $minLen . ',' . $maxLen . '}$/iu';
			break;
		case 'CN' :
			$match = '/^[_\x{4e00}-\x{9fa5}\d]{' . $minLen . ',' . $maxLen . '}$/iu';
			break;
		default :
			$match = '/^[_\w\d\x{4e00}-\x{9fa5}]{' . $minLen . ',' . $maxLen . '}$/iu';
	}
	return preg_match ( $match, $value );
}
/**
 * 验证真实姓名
 *
 * @param string $value        	
 * @param int $length        	
 * @return boolean
 */
function common_is_realnames($value, $minLen = 2, $maxLen = 20, $charset = 'ALL') {
	if (empty ( $value ))
		return false;
	switch ($charset) {
		case 'EN' :
			$match = '/^[a-zA-Z]{' . $minLen . ',' . $maxLen . '}$/iu';
			break;
		case 'CN' :
			$match = '/^[_\x{4e00}-\x{9fa5}]{' . $minLen . ',' . $maxLen . '}$/iu';
			break;
		default :
			$match = '/^[_\w\d\x{4e00}-\x{9fa5}]{' . $minLen . ',' . $maxLen . '}$/iu';
	}
	return preg_match ( $match, $value );
}
/**
 * 验证密码
 *
 * @param string $value        	
 * @param int $length        	
 * @return boolean
 */
function common_is_password($value, $minLen = 5, $maxLen = 16) {
	$match = '/^[\\~!@#$%^&*()-_=+|{}\[\],.?\/:;\'\"\d\w]{' . $minLen . ',' . $maxLen . '}$/';
	$v = trim ( $value );
	if (empty ( $v )) {
		return false;
	} else {
		return preg_match ( $match, $v );
	}
	
}

 

/**
 * 判断整数数处理方法
 * @param unknown $number
 * @return boolean|number
 */
function common_is_number($number, $min=1, $max=''){
	if(empty($number)) return false;
	$match = '/^([0-9]{'.$min.','.$max.'})$/is';
	return preg_match($match,$number);
}

/**
 * 判断浮点数处理方法
 * @param unknown $number
 * @return boolean|number
 */
function common_is_figure($number){
	if(empty($number)) return false;
	$match = '/^\d+\.?\d+$|^\d+$/is';
	return preg_match($match,$number);
}

/**
 * 校验日期处理方法
 * @param unknown $date
 * @return boolean|number
 */
function common_is_date($date)
{
	
	if ( empty($date) ) return false;
	
	$match = '/^\d{4}-\d{2}-\d{2} (\d{2}:\d{2}:\d{2})|(\d{2}:\d{2})$/s';
	
	return preg_match($match, $date);
	
}


/**
 * 原样打印数组函数
 * 便于调试查看
 */
function P($arr=''){
	header('Content-type:text/html;charset=utf-8');
	echo "<pre>";
	print_r($arr);
    echo "</pre>";
    die();
}

/**
 * 过滤数组处理方法
 */
function common_html_Arr($arr){
	foreach ( $arr as $k=>$v ){
		if( !is_array($v) ){
		 	$arr[$k] = htmlspecialchars($v,ENT_QUOTES);
		}else{
			$arr[$k] = common_html_Arr($v);
		}
	}
	return $arr;
}


/**
 * 去除数组空值方法 
 */
function common_filtration(array $arr){
	
	$arr = array_filter($arr,function($v){
	
		if ( $v == NULL && $v !== 0 )
		{
			return false;
		}
	
		return true;
	
	});
	
	return $arr;
	
}




/**
 * 判断访问的设备是否是手机端
 * @return boolean
 */
function common_web_is_mobile()
{ 
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
    {
        return true;
    } 
    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER['HTTP_VIA']))
    { 
        // 找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    } 
    // 脑残法，判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER['HTTP_USER_AGENT']))
    {
        $clientkeywords = array ('nokia',
            'sony',
            'ericsson',
            'mot',
            'samsung',
            'htc',
            'sgh',
            'lg',
            'sharp',
            'sie-',
            'philips',
            'panasonic',
            'alcatel',
            'lenovo',
            'iphone',
            'ipod',
            'blackberry',
            'meizu',
            'android',
            'netfront',
            'symbian',
            'ucweb',
            'windowsce',
            'palm',
            'operamini',
            'operamobi',
            'openwave',
            'nexusone',
            'cldc',
            'midp',
            'wap',
            'mobile'
            ); 
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
        {
            return true;
        } 
    } 
    // 协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT']))
    { 
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
        {
            return true;
        } 
    } 
    return false;
} 

/**
 * 上传图片
 * @param unknown $path
 * @param string $thumb
 * @return NULL|Ambigous <multitype:, multitype:unknown >
 */
function  common_upload_img($path,$thumb=false){
	$path='./'.$path;
	import('ORG.Net.UploadFile');//引入tp上传文件类
	$upload = new UploadFile();// 实例化上传类
	$upload->maxSize  =10485760;// 设置附件上传大小
	if($thumb){
		$upload->thumb=true;
		$upload->thumbMaxWidth=1024;
		$upload->thumbMaxHeight=768;
		$upload->thumbType = 0;//按等比例缩放
	}
	$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	$upload->savePath = $path;// 设置附件上传目录
     // die($path);     	
    if(!file_exists($path))
  	{
      mkdir($path);   	
    }
                  
	
	if(!$upload->upload()) {// 上传错误提示错误信息
		
		return null;//$upload->getErrorMsg();//$upload->getErrorMsg();
		
	}else{// 上传成功 获取上传文件信息
		
		return  $upload->getUploadFileInfo();
		
		
  }
}
/**
 * 上传音乐
 * @param unknown $path
 * @param string $thumb
 * @return NULL|Ambigous <multitype:, multitype:unknown >
 */
function  common_upload_music($path,$thumb=false){
	$path='./'.$path;
	import('ORG.Net.UploadFile');//引入tp上传文件类
	$upload = new UploadFile();// 实例化上传类
	$upload->maxSize  =10485760;// 设置附件上传大小
	$upload->allowExts  = array('MP3', 'mp3', 'WMA', 'wma','MP4','mp4','wav');// 设置附件上传类型
	$upload->savePath = $path;// 设置附件上传目录
	//die($path);
	if(!file_exists($path))
	{
		mkdir($path);
	}

	//ini_set('upload_max_filesize', '10M');
	
	//die(var_dump(ini_get('upload_max_filesize')));
	if(!$upload->upload()) {// 上传错误提示错误信息
		//die(P($upload->getErrorMsg()));
		return null;

	}else{// 上传成功 获取上传文件信息

		return  $upload->getUploadFileInfo();


	}
}

function common_get_pageMark($page, $pageCount=10)
{
	
	$page = $page - 1;
	
	$prev = $page * $pageCount;
	
	$pageStr = $prev.','.$pageCount;
	
	return $pageStr;
	
}

 /**
  *省略号
  */
function common_subtext($text, $length)
 {
    if(mb_strlen($text, 'utf8') > $length)
    	 
    return mb_substr($text, 0, $length, 'utf8').'...';
    
    return $text;
 }
 
 /**
  * 获取请求ip
  * @return Ambigous <string, unknown>
  */
 function common_get_Ip()
 {
 	$onlineip='';
 	
 	if( getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'),'unknown') )
 	{
 		$onlineip=getenv('HTTP_CLIENT_IP');
 	} 
 	elseif( getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),'unknown') )
 	{
 		$onlineip=getenv('HTTP_X_FORWARDED_FOR');
 		
 	} 
 	elseif( getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'),'unknown') )
 	{
 		$onlineip=getenv('REMOTE_ADDR');
 		
 	} 
 	elseif( isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'],'unknown') )
 	{
 		$onlineip=$_SERVER['REMOTE_ADDR'];
 	}
 	
 	return $onlineip;
 }
 
/**
 * 获取星期
 * @param unknown $time
 * @return Ambigous <string>|boolean
 */
 function common_get_weekday($time)
 {
 
 	if(is_numeric($time))
 
    {
 
        $weekday = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六');
 
        return $weekday[date('w', $time)];
 
    }
 
    return false;
  }
 